# coding:utf-8
import tkinter as tk

import ttkbootstrap as tkBoot

from ui.about_tab import AboutTab
from ui.class_tab import ClassTab
from ui.rand_name_tab import RandNameTab
from util.ui_util import show_center, inTabs, addToTabs


class MainBox(tkBoot.Window):
    def __init__(self):
        super().__init__(iconphoto="images/icon.png")
        self.notebook = tkBoot.Notebook(self, bootstyle="info")

    def initFrame(self):
        self.title("随机点名程序")
        win_width = int(self.winfo_screenwidth() / 2)
        win_height = int(self.winfo_screenheight() * 3 / 5)
        show_center(self, win_width, win_height)
        self.resizable(False, False)

        mainMenu = tk.Menu(self)
        openMenu = tk.Menu(mainMenu)
        openMenu.add_command(label="新建班级", command=lambda: self.openTab("classTab"))
        openMenu.add_command(label="退出程序", command=lambda: self.destroy())
        mainMenu.add_cascade(label="新建", menu=openMenu)
        helpMenu = tk.Menu(mainMenu)
        helpMenu.add_command(label="关于", command=lambda: self.openTab("aboutTab"))
        mainMenu.add_cascade(label="帮助", menu=helpMenu)
        self.config(menu=mainMenu)

        self.notebook.pack(fill="both", expand=True)

        self.openTab("randNameTab")

        self.mainloop()

    def openTab(self, tabName):
        if tabName is None or len(tabName.strip()) == 0:
            return
        newTab = None
        if tabName == "classTab":
            if not inTabs(tabName):
                newTab = ClassTab(self.notebook)
        elif tabName == "randNameTab":
            if not inTabs(tabName):
                newTab = RandNameTab(self.notebook)
        elif tabName == "aboutTab":
            if not inTabs(tabName):
                newTab = AboutTab(self.notebook)

        if newTab is not None:
            newTab.initFrame()
            addToTabs(tabName, newTab)
